package com.example.calories;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.TextViewCompat;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.haibin.calendarview.CalendarView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main3Activity extends AppCompatActivity implements View.OnClickListener {
    private Button back;
    private TextView textView[];
    Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bundle = getIntent().getBundleExtra("bundle");
        setContentView(R.layout.activity_main3);
        init();
        try {
            setText();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public void setText() throws ParseException {
        DatabaseHelper databaseHelper = new DatabaseHelper(this, "EX2", null, 1);
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String begin = bundle.getString("begin");
        begin = Splits(begin);
        String end = bundle.getString("end");
        end = Splits(end);
        System.out.println(begin+end);
      //  Cursor cursor = db.query("calories", null, "times>=" + begin + "and times<=" + end, null, null, null, null);
       //Cursor cursor = db.rawQuery("select * from calories where times>="+begin+"and times <="+end, new String[]{"calorie","times"});
       Cursor cursor = db.rawQuery("select * from calories where times>=?and times<=?",new String[]{begin,end});
        int i = 0;
        while (cursor.moveToNext()) {
            String calorie = cursor.getString(cursor.getColumnIndex("calorie"));
            String times = cursor.getString(cursor.getColumnIndex("times"));
            textView[i].setText(times + ":" + calorie);
            i++;
        }
    }

    public void init() {
        back = findViewById(R.id.back);
        back.setOnClickListener(this);
        textView = new TextView[17];
        textView[0] = findViewById(R.id.top1);
        textView[1] = findViewById(R.id.top2);
        textView[2] = findViewById(R.id.top3);
        textView[3] = findViewById(R.id.top4);
        textView[4] = findViewById(R.id.top5);
        textView[5] = findViewById(R.id.top6);
        textView[6] = findViewById(R.id.top7);
        textView[7] = findViewById(R.id.top8);
        textView[8] = findViewById(R.id.top9);
        textView[9] = findViewById(R.id.top10);
        textView[10] = findViewById(R.id.top11);
        textView[11] = findViewById(R.id.top12);
        textView[12] = findViewById(R.id.top13);
        textView[13] = findViewById(R.id.top14);
        textView[14] = findViewById(R.id.top15);
        textView[15] = findViewById(R.id.top16);
        textView[16] = findViewById(R.id.top17);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Main3Activity.this, MainActivity.class);
        startActivity(intent);
    }

    public String Splits(String s) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(formatter.parse(s));
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        String week = sdf.format(c.getTime());
        System.out.println(week);
       String newS = s+"-"+week;
       return newS;
    }

}
